/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: test123
 * Author: zelonghu
 * Date: 2019/3/29 4:41 PM
 * Description: 123
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
package com.example.zelonghu.test_screen_onoff;

import android.app.Notification;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import DateUtils.dateutils;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.io.SyncFailedException;

public class MainActivity extends AppCompatActivity {
    private long starttime;
    private long endtime;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        registerScreenActionReceiver();
    }

    private void registerScreenActionReceiver() {
        final IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION);
        registerReceiver(receiver, filter);
    }

    private final BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(final Context context, final Intent intent) {
            // Do your action here
            Log.i("屏幕开关","1234" + intent.getAction());
            String action = intent.getAction();
            if(action.equals("android.intent.action.SCREEN_OFF")){
                //关屏幕
                endtime = System.currentTimeMillis();
                long dur = endtime - starttime;
                Log.i("时间测定","" + dateutils.stampToDate(dur + "","HH:mm:ss"));
            }else{
                //开屏幕
                starttime = System.currentTimeMillis();
            }
        }
    };

}
